﻿using System.Configuration;

namespace App.Tools.Caches
{
    public class appCache
    {
        private static ICache? _cache = null;

        /// <summary>
        /// 静态构造函数， 只执行一次
        /// </summary>
        static appCache()
        {
            var typeStr = ConfigurationManager.AppSettings.Get("cacheType");

            if (typeStr == null)
            {
                typeStr = "memory";
            }
            var type = typeStr.ToString();

            if (string.IsNullOrWhiteSpace(type))
            {
                switch (type)
                {
                    case "memory":
                        _cache = Activator.CreateInstance(typeof(appMemoryCache)) as ICache ?? throw new Exception("未实例化appMemoryCache");
                        break;

                    default:
                        _cache = Activator.CreateInstance(typeof(appMemoryCache)) as ICache ?? throw new Exception("未实例化appMemoryCache");
                        break;
                }
            }
            else
            {
                _cache = Activator.CreateInstance(typeof(appMemoryCache)) as ICache ?? throw new Exception("未实例化appMemoryCache");
            }
        }
        public static void Add(string key, object data)
        {
            _cache.Add(key, data);
        }
        public static void Add(string key, object data, int cacheSecondTime)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("key 不能为空");
            }

            if (data == null)
            {
                throw new ArgumentException("data 不能为空");
            }

            _cache.Add(key, data, cacheSecondTime);
        }
        public static bool Contains(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("key 不能为空");
            }

            return _cache.Contains(key);
        }
        public static string Get(string key)
        {
            return Get<string>(key);
        }
        public static T Get<T>(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("key 不能为空");
            }

            return _cache.Get<T>(key);
        }
        public static void Remove(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("key 不能为空");
            }

            _cache.Remove(key);
        }
        public static async Task RemoveDelay(string key, int millisecondsDelay = 500)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("key 不能为空");
            }

            await Task.Delay(millisecondsDelay);
            _cache.Remove(key);
        }
        public static void Remove(List<string> keys)
        {
            if (keys == null || keys.Count == 0)
            {
                throw new ArgumentException("key 不能为空");
            }

            _cache.Remove(keys);
        }
        public static async Task RemoveDelay(List<string> keys, int millisecondsDelay = 500)
        {
            if (keys == null || keys.Count == 0)
            {
                throw new ArgumentException("key 不能为空");
            }

            await Task.Delay(millisecondsDelay);
            _cache.Remove(keys);
        }
        public static void RemoveAll()
        {
            if (_cache.Count == 0)
            {
                return;
            }

            _cache.RemoveAll();
        }

    }
}

